package UI_vente;

import GestionBilleterie.Tarif;
import GestionBilleterie.Vendeur;
import GestionPlannification.Genre;
import GestionPlannification.Piece;
import GestionPlannification.Representation;
import UI.Frame_accueil;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollBar;
/**
 *
 * @author  Elsa Ferrare, Alexandre Becquet
 * 
 */
public class Frame_vente extends javax.swing.JFrame {
    private DefaultListModel listModel;
    private List<Piece> lesPieces;
    private Vendeur monVendeur;
    private Frame_accueil maFrameAccueil;

    /** Creates new form Frame_vente */
    public Frame_vente() {
        initComponents();

        this.jButtonDeconnexion.setVisible(false);
        this.jLabelVendeur.setVisible(false);

        //mise en plein ecran
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle bounds = graphicsEnvironment .getMaximumWindowBounds();
        this.setBounds(bounds);

        //chargement des genres
        listModel = new DefaultListModel();
        List l=Genre.findEntities();
        listModel.addElement("Tous les genres");
        for(int i=0;i<l.size();i++){
            listModel.addElement(l.get(i));
        }
        this.jListPieces.setModel(listModel);

        //chargement des pièces
        chargerlesPieces();
    }

    public Frame_vente(Vendeur unVendeur, Frame_accueil accueil) {
        this.maFrameAccueil=accueil;
        this.monVendeur=unVendeur;
        initComponents();

        this.jLabelVendeur.setText("Vendeur : "+unVendeur.toString());

        //mise en plein ecran
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle bounds = graphicsEnvironment .getMaximumWindowBounds();
        this.setBounds(bounds);

        //chargement des genres
        listModel = new DefaultListModel();
        List l=Genre.findEntities();
        listModel.addElement("Tous les genres");
        for(int i=0;i<l.size();i++){
            listModel.addElement(l.get(i));
        }
        this.jListPieces.setModel(listModel);

        //chargement des pièces
        chargerlesPieces();
    }

    public void afficherPieces(){
        this.jPanel2.removeAll();
        int nbPieces=lesPieces.size();
        if(nbPieces>0){
            this.jPanel2.setLayout(new GridLayout(nbPieces, 1));
            for(int i=0;i<nbPieces;i++){
                Panel_Piece p=new Panel_Piece(this,lesPieces.get(i));
                this.jPanel2.add(p);
            }
        }
        else{
            this.jPanel2.add(new JLabel("Aucune pièces correspondantes aux critères"));
        }
        this.jPanel2.revalidate();
    }

    public void chargerlesPieces(){
        lesPieces=Piece.findEntities();
        afficherPieces();
    } 
    
    public void changerGenre(Genre genre){
        lesPieces=Piece.searchPiece(genre);
        afficherPieces();
    }

    void validationReservation(Representation representationSelect, Tarif tarifSelect, int nbPlaceReserv) {
        Panel_Validation pv=new Panel_Validation(this,representationSelect,tarifSelect,monVendeur,nbPlaceReserv);
        this.jPanel2.removeAll();
        this.jPanel2.add(pv);
        this.jPanel2.revalidate();
    }
    
    private void rechercheTexte(String text) {
        lesPieces=Piece.searchPiece(text);
        afficherPieces();
    }

    public void voirPiece(Piece p){
        Panel_voirPiece pv=new Panel_voirPiece(p,this);
        this.jPanel2.removeAll();       
        this.jPanel2.add(pv);
        this.jPanel2.revalidate();

        //mise du scrollbar en haut de la page
        JScrollBar verticalScrollBar = this.jScrollPane1.getVerticalScrollBar();
        JScrollBar horizontalScrollBar = this.jScrollPane1.getHorizontalScrollBar();
        verticalScrollBar.setValue(verticalScrollBar.getMinimum());
        horizontalScrollBar.setValue(horizontalScrollBar.getMinimum());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelRecherche = new javax.swing.JLabel();
        jTextFieldRecherche = new javax.swing.JTextField();
        jButtonRecherche = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jListPieces = new javax.swing.JList();
        jLabelVendeur = new javax.swing.JLabel();
        jButtonDeconnexion = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(51, 0, 0));
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(102, 0, 0));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabelRecherche.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabelRecherche.setForeground(new java.awt.Color(255, 255, 255));
        jLabelRecherche.setText("Recherche");
        jLabelRecherche.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel1.add(jLabelRecherche, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 160, 184, -1));

        jTextFieldRecherche.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldRechercheActionPerformed(evt);
            }
        });
        jPanel1.add(jTextFieldRecherche, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 180, 180, -1));

        jButtonRecherche.setBackground(new java.awt.Color(255, 255, 102));
        jButtonRecherche.setText("OK");
        jButtonRecherche.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRechercheActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonRecherche, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 210, -1, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Pièce par genre");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        jListPieces.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListPieces.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListPiecesValueChanged(evt);
            }
        });
        jPanel1.add(jListPieces, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 20, 180, 130));

        jLabelVendeur.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabelVendeur.setForeground(new java.awt.Color(255, 255, 255));
        jLabelVendeur.setText("Vendeur");
        jPanel1.add(jLabelVendeur, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 240, -1, -1));

        jButtonDeconnexion.setBackground(new java.awt.Color(255, 255, 102));
        jButtonDeconnexion.setText("Deconnexion");
        jButtonDeconnexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeconnexionActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonDeconnexion, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 260, -1, -1));

        jScrollPane1.setOpaque(false);

        jPanel2.setBackground(new java.awt.Color(255, 255, 102));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 453, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 295, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(459, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(188, 188, 188)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jListPiecesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListPiecesValueChanged
    if (evt.getValueIsAdjusting() == false) {
        this.jPanel2.removeAll();
        if(((JList)evt.getSource()).getSelectedIndex()==0){
            chargerlesPieces();
        }else{
            changerGenre(((Genre)((JList)evt.getSource()).getSelectedValue()));
        }
        this.jPanel2.revalidate();
    }
}//GEN-LAST:event_jListPiecesValueChanged

private void jButtonRechercheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRechercheActionPerformed
// TODO add your handling code here:
    this.jPanel2.removeAll();
    rechercheTexte(this.jTextFieldRecherche.getText());
    this.jPanel2.revalidate();
}//GEN-LAST:event_jButtonRechercheActionPerformed

private void jTextFieldRechercheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldRechercheActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldRechercheActionPerformed

private void jButtonDeconnexionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeconnexionActionPerformed
    this.dispose();
    this.maFrameAccueil.setVisible(true);
}//GEN-LAST:event_jButtonDeconnexionActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDeconnexion;
    private javax.swing.JButton jButtonRecherche;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelRecherche;
    private javax.swing.JLabel jLabelVendeur;
    private javax.swing.JList jListPieces;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldRecherche;
    // End of variables declaration//GEN-END:variables


}
